/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dto.Role;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.SQLServerDB;

/**
 *
 * @author vanchung07
 */
public class RoleDAO {

    private String lastError;
    private final String SQL_CREATE = "INSERT INTO Roles Values(?, ?)";
    private final String SQL_READALL = "SELECT * FROM Roles";
    private final String SQL_READBYID = "SELECT * FROM Roles WHERE ID = ?";
    private final String SQL_UPDATE = "UPDATE Roles SET Name = ?, Description = ? WHERE ID = ?";
    private final String SQL_DELETE = "DELETE FROM Roles WHERE ID = ?";
    private Connection conn;

    public RoleDAO() {
    }

    public boolean create(Role r) {
        conn = SQLServerDB.getConn();
        try {
            PreparedStatement pr = conn.prepareStatement(SQL_CREATE);
            pr.setString(1, r.getName());
            pr.setString(2, r.getDescript());
            if (pr.executeUpdate() > 0) {
                setLastError("Create Successful!");
                return true;
            } else {
                setLastError("Error: Create Failed!");
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return false;
    }

    public ArrayList<Role> ReadAll() {
        conn = SQLServerDB.getConn();
        ArrayList<Role> list = new ArrayList<>();
        try {
            PreparedStatement pr = conn.prepareStatement(SQL_READALL);
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                Role r = new Role();
                r.setId(rs.getInt("ID"));
                r.setName(rs.getString("Name"));
                r.setDescript(rs.getString("Description"));
                list.add(r);
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return list;
    }

    public Role readById(int id) {
        conn = SQLServerDB.getConn();
        Role r = new Role();
        try {
            PreparedStatement pr = conn.prepareStatement(SQL_READBYID);
            pr.setInt(1, id);
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                r.setId(rs.getInt("ID"));
                r.setName(rs.getString("Name"));
                r.setDescript(rs.getString("Description"));
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return r;
    }

    public boolean update(Role r) {
        conn = SQLServerDB.getConn();
        try {
            PreparedStatement pr = conn.prepareStatement(SQL_UPDATE);
            pr.setString(1, r.getName());
            pr.setString(2, r.getDescript());
            pr.setInt(3, r.getId());
            if (pr.executeUpdate() > 0) {
                setLastError("Update Successful!");
                return true;
            } else {
                setLastError("Error: Update Failed!");
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return false;
    }

    public boolean delete(int id) {
        conn = SQLServerDB.getConn();
        try {
            PreparedStatement pr = conn.prepareStatement(SQL_DELETE);
            pr.setInt(1, id);
            if (pr.executeUpdate() > 0) {
                setLastError("Delete Successful!");
                return true;
            } else {
                setLastError("Error: Delete Failed!");
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return false;
    }

    /**
     * @return the lastError
     */
    public String getLastError() {
        return lastError;
    }

    /**
     * @param lastError the lastError to set
     */
    public void setLastError(String lastError) {
        this.lastError = lastError;
    }
}
